#include "clientsearch.h"

UserSearch::UserSearch(QWidget *p)
    : QWidget(p)
{
    search_line = new LineEdit(this);
    search_line->setFixedSize(300, 22);
    search_line->move(5, 10);
    connect(search_line, SIGNAL(textChanged(QString)), SLOT(enableSearchBut()));
    QPixmap pix_for_search(":/icons/search(4).png");
    QPixmap pix_for_param(":/icons/settings(5).png");
    QPixmap pix_for_mode(":/icons/lock.png");
    search_but = new PushButton(this);
    search_but->setToolTip(QString::fromLocal8Bit("Почати пошук"));
    search_but->setFixedSize(pix_for_search.size());
    search_but->setIcon(QIcon(pix_for_search));
    search_but->setIconSize(search_but->size());
    search_but->move(306, 10);
    connect(search_but, SIGNAL(clicked()), SLOT(userSearch()));
    param_search_but = new PushButton(this);
    param_search_but->setToolTip(QString::fromLocal8Bit("Параметри пошуку"));
    param_search_but->setCheckable(true);
    param_search_but->setFixedSize(pix_for_param.size());
    param_search_but->setIcon(QIcon(pix_for_param));
    param_search_but->setIconSize(param_search_but->size());
    param_search_but->move(356, 10);
    connect(param_search_but, SIGNAL(clicked()), SLOT(paramSearchWid()));
    mode_but = new PushButton(this);
    mode_but->setToolTip(QString::fromLocal8Bit("Заблокувати редагування таблиці"));
    mode_but->setCheckable(true);
    mode_but->setChecked(true);
    mode_but->setIcon(QIcon(pix_for_mode));
    mode_but->setFixedSize(pix_for_mode.size());
    mode_but->setIconSize(mode_but->size());
    mode_but->move(331, 10);
    connect(mode_but, SIGNAL(clicked()), SLOT(modeSearch()));
    g_b = new QGroupBox(QString::fromLocal8Bit("Параметри пошуку"), 0);
    QVBoxLayout *v_lay = new QVBoxLayout(g_b);
    all_search = new QRadioButton(QString::fromLocal8Bit("відобразити всі записи"));
    all_search->setObjectName("all");
    connect(all_search, SIGNAL(clicked()), SLOT(setSearchParam()));
    v_lay->addWidget(all_search);
    name_search = new QRadioButton(QString::fromLocal8Bit("по імені або ініціалах"));
    name_search->setObjectName("inicial");
    name_search->setChecked(true);
    v_lay->addWidget(name_search);
    connect(name_search, SIGNAL(clicked()), SLOT(setSearchParam()));
    login_search = new QRadioButton(QString::fromLocal8Bit("по логіну"));
    login_search->setObjectName("login");
    v_lay->addWidget(login_search);
    connect(login_search, SIGNAL(clicked()), SLOT(setSearchParam()));
    id_search = new QRadioButton(QString::fromLocal8Bit("по ідентифікатору в базі даних"));
    id_search->setObjectName("id");
    v_lay->addWidget(id_search);
    connect(id_search, SIGNAL(clicked()), SLOT(setSearchParam()));
    phone_search = new QRadioButton(QString::fromLocal8Bit("по номеру телефона"));
    phone_search->setObjectName("phone");
    v_lay->addWidget(phone_search);
    connect(phone_search, SIGNAL(clicked()), SLOT(setSearchParam()));
    street_search = new QRadioButton(QString::fromLocal8Bit("по назві вулиці"));
    street_search->setObjectName("street");
    v_lay->addWidget(street_search);
    connect(street_search, SIGNAL(clicked()), SLOT(setSearchParam()));
    dev_type_search = new QRadioButton(QString::fromLocal8Bit("по типу обладнання"));
    dev_type_search->setObjectName("type_machine");
    v_lay->addWidget(dev_type_search);
    connect(dev_type_search, SIGNAL(clicked()), SLOT(setSearchParam()));
    tos_search = new QRadioButton(QString::fromLocal8Bit("по типу сервісу"));
    tos_search->setObjectName("service_type");
    v_lay->addWidget(tos_search);
    connect(tos_search, SIGNAL(clicked()), SLOT(setSearchParam()));
    num_port_search = new QRadioButton(QString::fromLocal8Bit("по номеру порта"));
    num_port_search->setObjectName("port_number");
    v_lay->addWidget(num_port_search);
    connect(num_port_search, SIGNAL(clicked()), SLOT(setSearchParam()));
    g_b->setLayout(v_lay);
    model = new SqlQueryModel;   
    database_view = new QTableView(this);
    database_view->hide();
    database_view->move(5, 36);
    database_view->setModel(model);
    database_blocked_view = new QTableView(this);
    database_blocked_view->hide();
    database_blocked_view->move(5, 36);
    database_blocked_view->setModel(model);
    database_blocked_view->setEditTriggers(QTableView::NoEditTriggers);
    database_common_view = database_blocked_view;
    query = "all"; // за замовчуванянм виводиться всі дані таблиці
    model->setQueryType(query);
}

UserSearch::~UserSearch()
{
    delete g_b;
    delete model;
}

void UserSearch::paramSearchWid()
{
    if (param_search_but->isChecked())
    {
        g_b->move(param_search_but->pos().x() + 110, param_search_but->pos().y() + 80);
        g_b->show();
    }
    else g_b->hide();
}

void UserSearch::modeSearch()
{
    if (mode_but->isChecked())
    {
        database_view->hide();
        database_blocked_view->show();
        database_common_view = database_blocked_view;
    }
    else
    {
        database_view->show();
        database_blocked_view->hide();
        database_common_view = database_view;
    }
}

void UserSearch::enableSearchBut()
{
    if (query != "all") search_but->setEnabled(!search_line->text().isEmpty());
}

void UserSearch::mousePressEvent(QMouseEvent *ev)
{
    if (param_search_but->isChecked())
    {
        param_search_but->setChecked(false);
        g_b->hide();
    }
    QWidget::mousePressEvent(ev);
}

void UserSearch::setSearchParam()
{
    QString search_param = ((QRadioButton*)this->sender())->objectName();
    param_search_but->setChecked(false);
    g_b->hide();
    if (search_param == "all")
    {
        search_but->setEnabled(true);
        query = "all";
        model->setQueryType(query);
        return;
    }
    else if (search_param == "phone") query = "select clients.*, radcheck.username, radcheck.value from clients, radcheck "
            "where clients.home_phone='%1' or clients.mobile_phone='%1' and clients.id=radcheck.id;";
    else if (search_param == "inicial") query = "select clients.*, radcheck.username, radcheck.value from clients, radcheck "
                                                "where clients.inicial LIKE '%%1%' and clients.id=radcheck.id;";
    else query = QString("select clients.*, radcheck.username, radcheck.value from clients, radcheck "
                         "where %1='%2' and clients.id=radcheck.id;").arg(search_param);
    model->setQueryType("special");
    search_but->setEnabled(search_line->text().size());
}

void UserSearch::userSearch()
{
    database_view->resize(this->width(), this->height() - 100);
    database_blocked_view->resize(this->width(), this->height() - 100);
    if (query == "all") model->setQuery("select clients.*, radcheck.username, radcheck.value from clients, radcheck "
                                        "where clients.id=radcheck.id;");
    else model->setQuery(query.arg(search_line->text()));
    if (database_common_view->isHidden()) database_common_view->show();
}
